<template>
  <div class="search-box">
    <div class="search">
      <div class="search-l">
        <el-icon><Search /></el-icon>
        <span>筛选搜索</span>
      </div>
      <div class="search-r">
        <el-button @click="chongzhifn">重置</el-button>
        <el-button type="primary" @click="queryRoleList">查询搜索</el-button>
      </div>
    </div>
    <div>
      <span>角色名称: </span>
      <el-input
        placeholder="请输入角色名称"
        v-model="queryParams.keyword"
        style="width: 300px"
      ></el-input>
    </div>
  </div>
  <div class="add-area">
    <el-button type="primary" @click="addFn">添加</el-button>
  </div>
  <el-table :data="roleList" border cell-class-name="align-c">
    <el-table-column label="编号" prop="id" />
    <el-table-column label="角色名称" prop="name" />
    <el-table-column label="描述" prop="description" />
    <el-table-column label="用户数" prop="adminCount" />
    <el-table-column label="添加时间" prop="createTime">
      <template #default="{ row }">
        <span>{{ formateDate(row.createTime) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="是否启用" prop="status">
      <template #default="{ row }">
        <el-switch
          v-model="row.status"
          :active-value="1"
          :inactive-value="0"
          @change="switchStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          @click="router.push('/ums/allocMenu?id=' + row.id)"
          >分配菜单</el-button
        >
        <el-button
          text
          type="primary"
          @click="router.push('/ums/allocResource?id=' + row.id)"
          >分配资源</el-button
        >
        <el-button text type="primary" @click="editRole(row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="queryRoleList"
        @current-change="queryRoleList"
      />
    </div>
  </ElConfigProvider>

  <AddRole
    :show="showAllRoletan"
    @hide="closeDialog"
    :id="editId"
    :rocontent="editcontent"
  />
</template>
<script setup lang="ts">
import { Search } from "@element-plus/icons-vue";
import { formateDate } from "@/utils";
import { reactive, ref } from "vue";
import { getRolesListApi, roleStatusApi } from "./api";
// 国际化分页组件
import { ElConfigProvider, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { useRouter } from "vue-router";
import AddRole from "./addRole.vue";
const router = useRouter();

const roleList = ref<IRoleModel[]>([]);
const total = ref(0);

const queryParams = reactive<IUserListParams>({
  keyword: "",
  pageNum: 1,
  pageSize: 10,
});
// 重置按钮的方法
const chongzhifn = () => {
  queryParams.keyword = "";
  queryRoleList();
};
// 请求列表数据
const queryRoleList = () => {
  getRolesListApi(queryParams).then((res) => {
    if (res.code === 200) {
      roleList.value = res.data.list;
      total.value = res.data.total;
    }
  });
};
// 请求一开始的默认数据
queryRoleList();
// 切换是否禁用账号
const switchStatus = (row: IUserModel) => {
  roleStatusApi(row.id!, row.status)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("操作成功");
      } else {
        row.status = row.status === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      row.status = row.status === 1 ? 0 : 1;
    });
};
// 点编辑按钮的方法
const editRole = (row: IRoleModel) => {
  editId.value = row.id;
  editcontent.value.name = row.name;
  editcontent.value.description = row.description;
  editcontent.value.status = row.status;
  showAllRoletan.value = true;
};
// 这是添加按钮的方法
const addFn = () => {
  // editId.value = 0;
  showAllRoletan.value = true;
};
// 传给子组件的关闭弹窗事件
const closeDialog = (iRshow?: boolean) => {
  // 要重置数据，不然连续点击编辑，它第二次不会出现旧的数据，因为watch监听的值没有变化，不会触发watch发送请求显示旧的数据
  editId.value = 0;
  showAllRoletan.value = false;
  if (iRshow) queryRoleList();
};
// 控制分配角色的提示框显示和隐藏
const showAllRoletan = ref(false);

// 要传过去给子组件信息
const editId = ref(0);
const editcontent = ref({
  name: "",
  description: "",
  status: 0,
});
</script>
<style lang="less" scoped>
.search-box {
  box-shadow: 0 0 4px 2px #ccc;
  padding: 20px;
  border-radius: 10px;
  margin-bottom: 20px;
  .search {
    display: flex;
    justify-content: space-between;
    padding: 20px;
  }
}
.page {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
:deep(.align-c) {
  .cell {
    text-align: center;
  }
}

.add-area {
  display: flex;
  justify-content: flex-end;
  padding: 20px;
  margin-bottom: 20px;
}
</style>
